<!--
 * @Author: wangding
 * @Date: 2020-11-12 16:02:01
 * @LastEditTime: 2021-03-30 14:35:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\versionService\versionService.vue
-->
<template>
    <div class="versionService-main-box">
        <div class="search-box">
            <div class="search-box-sub">
                <span class="lable-text">{{$t('version.versionAdd.versionNum')}}</span>
                <el-input v-model="version" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('equipmentClassify')}}</span>
                <el-select v-model="type" filterable :placeholder="$t('pleaseSelect')" clearable @change='typeChange'>
                    <el-option
                        v-for="item in dictionary.DEVICE_CLASSIFY"
                        :key="item.value"
                        :label="item.code"
                        :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub" v-if='type == 4'>
                <span class="lable-text">{{$t('battery.firmName')}}</span>
                <el-select v-model="brand" filterable :placeholder="$t('pleaseSelect')" clearable @change='batterTypeChange'>
                    <el-option
                        v-for="item in dictionary.BATTERY_BRAND"
                        :key="item.value"
                        :label="item.code"
                        :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub" v-if='type'>
                <span class="lable-text">{{$t('version.versionAdd.compatible')}}</span>
                <el-select v-model="value" filterable :placeholder="$t('pleaseSelect')" clearable>
                    <el-option
                        v-for="item in valueList"
                        :key="item.value"
                        :label="item.code"
                        :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('version.versionService.publishStatus')}}</span>
                <el-select v-model="status" filterable :placeholder="$t('pleaseSelect')" clearable>
                    <el-option
                        v-for="item in dictionary.PUBLISH_STATUS"
                        :key="item.value"
                        :label="item.code"
                        :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <el-button type="primary" @click='query'>{{$t('query')}}</el-button>
            </div>
        </div>
        <div class="button-box">
            <el-button type="primary"  icon='el-icon-edit' class="marginR10" @click='addMoadl'>{{$t('add')}}</el-button>
        </div>
        <baseTable v-loading="loading"  row-key='sid' :data="dataRows" border  stripe :column='column' :select='false'>
            <template #path="{data}">
                <a href="javascript:;" class="hover-a-link" style="color:#ED7523;"  :title='data.path'>{{data.path | stringLength(15)}}</a>
            </template>
            <template #inverterType="{data}">
                <span>{{comm.returnCode(dictionary.DEVICE_CLASSIFY,data.inverterType)}}</span>
            </template>
            <template #scope="{data}">
                <span>{{comm.returnCode(returnValueCode(data.inverterType,data.brand),data.scope)}}</span>
            </template>
            <template #checkFlag="{data}">
                <span>{{returnFlag(data.checkFlag)}}</span>
            </template>
            <template #status="{data}">
                <span>{{returnStatus(data)}}</span>
            </template>
            <template #option="{data}">
                <div class="option-box">
                    <el-popconfirm
                            :confirm-button-text="$t('sure')"
                            :cancel-button-text="$t('cancel')"
                        :title="$t('device-confirm-verify')"
                        @confirm='updateCheckVersion(data.ids)'
                        v-if='showyanjing(data)'
                    >
                        <el-button class="marginR10" type="danger" slot="reference"  :title="$t('version.versionService.check')" icon='iconfont icon-yanjing'></el-button>
                    </el-popconfirm>

                    <el-popconfirm
                            :confirm-button-text="$t('sure')"
                            :cancel-button-text="$t('cancel')"
                        :title="$t('device-re-verify')"
                        @confirm='updateReCheckVersion(data.ids)'
                        v-if='closeyanjing(data)'
                    >
                        <el-button class="marginR10" type="danger" slot="reference"  :title="$t('version.versionAdd.Retrial')" icon='iconfont icon-jurassic_loseeyes' style="color:red;"></el-button>
                    </el-popconfirm>

                    <el-popconfirm
                            :confirm-button-text="$t('sure')"
                            :cancel-button-text="$t('cancel')"
                            :title="$t('version.versionService.forbiddenSure')"
                        @confirm='updateStopVersion(data.ids)'
                        v-if='showClose(data)'
                    >
                        <el-button class="marginR10" type="danger" slot="reference" icon='iconfont icon-jinyong' :title="$t('version.versionService.forbidden')"></el-button>
                    </el-popconfirm>

                    <el-popconfirm
                            :confirm-button-text="$t('sure')"
                            :cancel-button-text="$t('cancel')"
                            :title="$t('version.versionService.publishSure')"
                        @confirm='updateReleaseVersion(data.ids)'
                        v-if='showSend(data)'
                    >
                        <el-button class="marginR10" type="danger" slot="reference" icon='el-icon-s-promotion' :title="$t('version.versionService.publish')"></el-button>
                    </el-popconfirm>
                    
                    <el-popconfirm
                            :confirm-button-text="$t('sure')"
                            :cancel-button-text="$t('cancel')"
                        :title="$t('index.delete_item_confirm')"
                        @confirm='updateDeleteVersion(data.ids)'
                        v-if="data.status !=1"
                    >
                        <el-button class="marginR10" type="danger" slot="reference" icon='el-icon-delete' :title="$t('delete')"></el-button>
                    </el-popconfirm>

                    <el-button class="marginR10" type="danger" icon='el-icon-tickets' :title="$t('remark')" @click='showEditModal(data)'></el-button>
                    
                    <el-button v-if='data.status == 3 || (data.checkFlag==1&&data.status==1)'  type="danger" icon='iconfont icon-fenpei' :title="$t('version.service.Special')" @click='showSpecialModal(data)'></el-button>
                </div>
            </template>
        </baseTable>
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>

        <add @uploadData='query' v-if='newVisible'></add>
        <edit @uploadData='query' :id='editRowData.ids'></edit>
        <special @uploadData='query' :id='editRowData.ids'></special>
    </div>
</template>

<script>
import baseTable from '../../../components/common/baseTable'
import add from './add'
import edit from './edit'
import special from './special'

export default {
    components: {
        baseTable,
        add,
        edit,
        special
    },
    props: {

    },
    data() {
        return {
            inverterSn:'',
            dictionary:{},
            type:'',
            value:'',
            valueList:[],
            brand:'',
            size:10,
            current:1,
            total:1,
            version:'',
            status:'',
            loading:false,
            dataRows:[],
            column:[{
                title: this.$t('version.versionAdd.partNumber'),
                value: 'partNumber',
            },
            {
                title: this.$t('version.versionAdd.longVersion'),
                value: 'longVersion',
                slot: 'longVersion',
            },
            {
                title: this.$t('version.versionAdd.versionNum'),
                value: 'version',
                slot: 'version',
            },{
                title: this.$t('version.versionAdd.compatible'),
                value: 'scope',
                slot: 'scope',
            },
            {
                title: this.$t('equipmentClassify'),
                value: 'inverterType',
                slot: 'inverterType',
            },
            {
                title: this.$t('version.versionService.fileURL'),
                value: 'path',
                slot: 'path',
            },{
                title: this.$t('uploadtime'),
                value: 'uploadTime',
            },
            {
                title: this.$t('version.versionAdd.auditStatus'),
                value: 'checkFlag',
                slot: 'checkFlag',
                min_width:'100'
            },
            {
                title: this.$t('version.versionService.publishStatus'),
                value: 'status',
                slot: 'status',
                min_width:'100'
            },
            {
                title: this.$t('control'),
                value: 'sid',
                slot: 'option',
            }],
            roleCode:'',
            editRowData:''
        };  
    },
    computed: {
        newVisible(){
            return this.$store.state.sysStore.newVisible
        }
    },
    watch: {

    },
    created() {

    },
    mounted() {
        this.getDictionaryBatch()
        this.updateGetVersionPage()
    },
    methods: {
        showSpecialModal(data){
            this.editRowData = data
            this.$store.commit('setVisibleAdd',true)
        },
        showEditModal(data){
            this.editRowData = data
            this.$store.commit('setNewVisibleEdit',true)
        },
        async updateDeleteVersion(id){
            let res = await this.API.updateDeleteVersion({
                id
            })
            if(this.comm.showMsg(res.success)){
                this.updateGetVersionPage()
            }
        },
        async updateReCheckVersion(id){
            let res = await this.API.updateReCheckVersion({
                id
            })
            if(this.comm.showMsg(res.success)){
                this.updateGetVersionPage()
            }
        },
        async updateReleaseVersion(id){
            this.loading = true
            let res = await this.API.updateReleaseVersion({
                id
            })
            this.loading = false
            if(this.comm.showMsg(res.success)){
                this.updateGetVersionPage()
            }
        },  
        async updateStopVersion(id){
            this.loading = true
            let res = await this.API.updateStopVersion({
                id
            })
            this.loading = false
            if(this.comm.showMsg(res.success)){
                this.updateGetVersionPage()
            }
        },
        async updateCheckVersion(id){
            let res = await this.API.updateCheckVersion({
                id
            })
            if(this.comm.showMsg(res.success)){
                this.updateGetVersionPage()
            }
        },
        showClose(data){
            if(this.roleCode == 'other'){
                return false
            }else if(this.roleCode == 'test'){
                 if(data.status == 1){
                     return true
                 }else{
                     return false
                 }
            }
        },
        showyanjing(data){
            if(this.roleCode == 'other'){
                return false
            }else if(this.roleCode == 'test'){
                if(data.status == 1 || data.status == 2){
                    if(data.checkFlag == 1){
                        return true
                    }else{
                        return false
                    }
                }
            }
        },
        showSend(data){
            if(this.roleCode == 'other'){
                return false
            }else if(this.roleCode == 'test'){
                if(data.status == 0 || data.status == 2){
                    return true
                }else{
                    return false
                }
            }
        },
        closeyanjing(data){
            if(this.roleCode == 'other'){
                return false
            }else if(this.roleCode == 'test'){
                if(data.status == 1 || data.status == 2){
                    if(data.checkFlag == 1){
                        return false
                    }else{
                        return true
                    }
                }
            }
        },
        returnStatus(row){
            let name = ''
            if(row.checkFlag==1&&row.status==1){
                name = this.$t('version.versionService.Testing')
            }else{
                name = this.comm.returnCode(this.dictionary.PUBLISH_STATUS,row.status)
            }
            return name
        },
        returnFlag(val){
            let flagName = ''
            if(val == 0){
                flagName = this.$t('version.versionAdd.Audited')
            }else{
                flagName = this.$t('version.versionAdd.Notthrough')
            }
            return flagName
        },
        addMoadl(){
            this.$store.commit('setNewVisible',true)
        },
        async updateGetVersionPage(){
            this.loading = true
            let res = await this.API.updateGetVersionPage({
                size:this.size,
                current:this.current,
                version:this.version,
                type:this.type,
                brand:this.brand,
                value:this.value,
                status:this.status,
            })
            this.loading = false
            this.dataRows = res.rows
            this.total = res.total
            this.roleCode = res.roleCode
        },
        returnValueCode(type,batter){
            let arr = []
            if(type == '1'){
                arr = this.dictionary.INVERTER_TYPES
            }
            if(type == '2'){
                arr = this.dictionary.SCOPE_TO
            }
            if(type == '3'){
                arr = this.dictionary.INVERTER_TYPES
            }
            if(type == '6'){
                arr = this.dictionary.INVERTER_TYPES
            }
            if(type == '4'){
                if(batter == 81){
                    arr = this.dictionary.BATTERY_VERSION_81
                }
                if(batter == 82){
                    arr = this.dictionary.BATTERY_VERSION_82
                }
                if(batter == 83){
                    arr = this.dictionary.BATTERY_VERSION_83
                }
                if(batter == 84){
                    arr = this.dictionary.BATTERY_VERSION_84
                }
                if(batter == 129){
                    arr = this.dictionary.BATTERY_VERSION_129
                }
            }
            return arr
        },
        typeChange(){
            if(this.type == '1'){
                this.valueList = this.dictionary.INVERTER_TYPES
                this.brand = ''
            }
            if(this.type == '2'){
                this.brand = ''
                this.valueList = this.dictionary.SCOPE_TO
            }
            if(this.type == '3' || this.type == '5'|| this.type == '6'){
                this.brand = ''
                this.valueList = this.dictionary.INVERTER_TYPES
            }
            if(this.type == '4'){
                this.valueList = []
            }
            if(!this.type){
                this.brand = ''
                this.valueList = ''
                this.value = ''
                localStorage.setItem('token','')
            }
        },
        batterTypeChange(){
            if(this.brand == 81){
                this.valueList = this.dictionary.BATTERY_VERSION_81
            }
            if(this.brand == 82){
                this.valueList = this.dictionary.BATTERY_VERSION_82
            }
            if(this.brand == 83){
                this.valueList = this.dictionary.BATTERY_VERSION_83
            }
            if(this.brand == 129){
                this.valueList = this.dictionary.BATTERY_VERSION_129
            }
        },
        async getDictionaryBatch(){
            let res = await this.API.getDictionaryBatch({
                dictCodes:'SCOPE_TO,INVERTER_TYPES,DEVICE_CLASSIFY,PUBLISH_STATUS,BATTERY_VERSION,BATTERY_BRAND,BATTERY_VERSION_81,BATTERY_VERSION_82,BATTERY_VERSION_83,BATTERY_VERSION_84,BATTERY_VERSION_129',
                lang:localStorage.getItem('lang')||'zh_CN'
            })
            this.dictionary = res || {}
        },
        query(){
            this.current = 1
            this.updateGetVersionPage()
        },
        handleSizeChange(val) {
          this.size = val
          this.current = 1
          this.updateGetVersionPage()
        },
        handleCurrentChange(val) {
          this.current = val
          this.updateGetVersionPage()
        },
    },
};
</script>

<style  lang="less" scoped>

</style>
